<?php

/*
 * 驗證類，所有後台文件都要繼承這個類
 *
 *
 * @author base
 * @version 0.1
 *
 */
import("Admin.ORG.Page");

class AdminAction extends Action {

    protected $base_title = 'base_title';

    public function _initialize() {
        $this->checkLogin();
        header("Content-Type:text/html; charset=utf-8");
    }

    /* 显示管理员列表 */

    public function index() {
        $this->assign('base_title', "管理员列表");
        $this->getPage('Admin', '', 'adminList', 'admin_id desc');
        $this->display();
    }

    /**
     *  添加/修改
     *  修改时要传递一个id用post方式
     * @param 数据模型的名称 $model
     */
    public function save($model) {
        $postDao = D($model ? $model : $this->getActionName());
        $pv = $_POST;
        foreach ($pv as $key => $value) {
            if ($value == '') {
                unset($pv[$key]);
                continue;
            } else {
                $value = preg_replace("'([\r\n])[\s]+'", "", $value);
                $pv[$key] = str_replace("\r\n", "", $value);
            }
        }
        $vo = $postDao->create($pv);
        if (!$vo)
            $this->error($postDao->getError());
        $rs = false;
        $success = "";
        $error = "";
        if (!empty($_POST [$postDao->getPk()])) {
            $rs = $postDao->save();
            $success = "修改内容成功";
            $error = "修改内容失败";
        } else {
            $rs = $postDao->add();
            $success = "添加内容成功";
            $error = "添加内容失败";
        }
        if ($rs) {
            $this->success($success);
        } else {
            $this->error($error);
        }
    }

    /**
     * 删除
     * @param 数据模型 $model
     */
    public function delete($model) {
        $dao = D($model ? $model : $this->getActionName());
        if (isset($_POST [$dao->getPk()]) && is_array($_POST [$dao->getPk()])) {
            $ids = implode(",", $_POST [$dao->getPk()]);
            $dao->delete($ids);
        } else if (isset($_GET [$dao->getPk()]) && !empty($_GET [$dao->getPk()])) {
            $dao->delete($_GET[$dao->getPk()]);
        }
        $this->success("删除成功");
    }

    /**
     * 分页方法
     *
     * @param 数据模型 $model
     * @param 查询条件 $where
     * @param 模板的参数 $list
     * @param 排序的列参数 $order
     * @param 显示最多条数 $LIST_NUMBERS
     */
    public function getPage($model = NULL, $where = NULL, $list = NULL, $sortBy = NULL, $LIST_NUMBERS = NULL) {
        import("ORG.Util.Page"); //导入分页类
        $mail = "";
        $dao = D($model != NULL ? $model : $this->getActionName());
        $list = $list ? $list : 'list';
        $LIST_NUMBERS = $LIST_NUMBERS ? $LIST_NUMBERS : C('LIST_NUMBERS');
        //排序显示部分
        if (isset($_REQUEST['order'])) {
            $order = $_REQUEST['order'];
        } else {
            $order = !empty($sortBy) ? $sortBy : $dao->getPk();
        }
        if (isset($_REQUEST['sort'])) {
            $sort = $_REQUEST['sort'] ? 'asc' : 'desc';
        } else {
            $sort = $asc ? 'desc' : 'asc';
        }
        if (THINK_MODE == 'Compat') {
            $count = $dao->count($where); //获取总条数
        } else {
            $count = $dao->where($where)->count($dao->getPk());
        }
        $p = new Page($count, $LIST_NUMBERS); //生产分页类
        $res = $dao->order($order)->where($where)->limit($p->firstRow . ',' . $p->listRows)->findAll(); //查找
        if ($count > $LIST_NUMBERS) { //判断是否显示分页
            $page = $p->show();
            $this->assign('pagestyle', C('pagestyle')); //分页样式
            $this->assign('page', $page); //翻页链接
        }
        $sortImg = $sort;
        $sortAlt = $sort == 'desc' ? '倒序排列' : '升序排列';    //排序提示
        $sort = $sort == 'desc' ? 1 : 0;                     //排序方式
        //模板赋值显示
        $this->assign('sort', $sort);
        $this->assign('order', $order);
        $this->assign('sortImg', $sortImg);
        $this->assign('sortType', $sortAlt);
        $this->assign($list, $res);
//        dump($res);
        foreach ($res as $value) {
            $mail[] = $value['mail'];
        }
        $email = implode(",", $mail);
        $this->assign('getmail', $email);
        //echo $dao->getLastSql();
        unset($dao, $p, $count, $order, $model, $where, $LIST_NUMBERS);
    }

    protected function success($message, $ajax) {
        $this->assign('status', 1);
        parent::success($message, $ajax);
    }

    protected function error($message, $ajax) {
        $this->assign('status', 0);
        parent::error($message, $ajax);
    }

    //析构函数
    function __destruct() {
        unset($_POST, $_GET);
    }

    //检查登陆函数
    public function checkLogin() {
        if (!isset($_SESSION[C('ADMIN_SESSION_KEY')])) {
            $this->redirect('Public/login');
        }
    }

    public function verify() {
        import("Admin.ORG.Util.Image");
        Image::buildImageVerify(4, 1);
    }

}

?>